package com.caifu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.pojo.SysLog;
import com.caifu.pojo.SysLogLabel;
import com.caifu.pojo.SysUser;
import com.caifu.service.SysLogLabelService;
import com.caifu.service.SysLogService;
import com.caifu.service.SysUserService;
import com.caifu.util.Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 日志管理
 * 
 * @author szt
 * @date 2020年10月13日
 */
@Controller
@RequestMapping("/log")
public class LogController extends AbstractController{
	private static final Logger logger = LoggerFactory.getLogger(LogController.class);
	 @Autowired
	 private SysLogService logService;
	 @Autowired
	 private SysLogLabelService labelService;
	 @Autowired
	 private SysUserService userService;

	/**
	 * 进入日志管理
	 * 192.168.1.180:9999/caifu/log/listPage
	 * 2020年10月13日
	 * codeBy szt
	 */
	@RequestMapping("/listPage")
	public String listPage(String initPage, ModelMap map){
		try {
			initPage="1";
			List<SysLogLabel> labelList = labelService.list(new QueryWrapper<SysLogLabel>().orderByAsc("sort"));
			map.put("initPage",initPage);
			map.put("labelList",labelList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "view/log/logList";
	}

	/**
	 * 获取日志列表
	 * 192.168.1.180:9999/caifu/log/getLogListData?currentPage=1
	 * 2020年10月13日
	 * codeBy szt
	 */
	@PostMapping("getLogListData")
	@ResponseBody
	public Result getLogListData(int currentPage, String startDate, String endDate, String functionType){
		try {
			QueryWrapper wp = new QueryWrapper<SysLog>();
			wp.like("function_type","sys");
			if(StringUtils.isNotBlank(functionType)){
				wp.eq("function_type",functionType);
			}
			if(StringUtils.isNotBlank(startDate)&&StringUtils.isNotBlank(endDate)){
				startDate = startDate+"000000";
				endDate = endDate+"235959";
				System.out.println(Util.stringToDate(startDate,"yyyyMMddHHmmss"));
				wp.ge("create_time",Util.stringToDate(startDate,"yyyyMMddHHmmss"));
				wp.le("create_time",Util.stringToDate(endDate,"yyyyMMddHHmmss"));
			}
			wp.orderByDesc("create_time");
			IPage page = logService.pageMaps(new Page<SysLog>(currentPage,Util.PAGE_SIZE_TEN),wp);
			List<Map<String,Object>> logList = page.getRecords();
			logList.forEach(model->{
				String tmpFunctionType = (String) model.get("function_type");
				SysLogLabel label = labelService.getOne(new QueryWrapper<SysLogLabel>().eq("label_no",tmpFunctionType));
				if(label!=null){
					model.put("function_type",label.getLabelName());
				}
				int tmpUserNo = (int) model.get("user_no");
				SysUser user = userService.getById(tmpUserNo);
				if(user!=null){
					model.put("user_no",user.getLoginName());
				}
			});
			return ResultUtil.success(200, "获取日志列表数据成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(199,"获取日志列表数据失败！");
		}
	}
}
